// page-billOfDocumentImg/bodImghome.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.requestURL +'upload/uploadImgTest',
    path: '', //文件本地路径
    name: '', //name标识符
    imagesType: '', //自定义上传的目录路径
    identityNum: '', //身份证号
    certificate: wx.getStorageSync("certificate"), //合格证书或档案证书
    invoice: wx.getStorageSync("invoice"), //采购发票照片
    purchaseContract: wx.getStorageSync("purchaseContract"), //采购合同照片
    purchaseContractSec: wx.getStorageSync("purchaseContractSec"),
    bg_certificate: '', //合格证书或档案证书
    bg_invoice: '', //采购发票照片
    bg_purchaseContract: '', //采购合同照片
    bg_purchaseContractSec: '',//背景
    bigShelfNum: '',//设备出厂编号
    qylxr: false,
    payData: {
      openid: wx.getStorageSync('openid'),
      total_fee: 110000,
      body: "设备登记"
    },
    isSubmit: true
  },
  chooseImage: function (e) {
    console.log(e.target.id)
    var self = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'],
      success: function (res) {
        if (e.target.id == '0') {
          self.setData({
            name: "purchaseContract",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.bigShelfNum + '/' + "shouxu"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync("purchaseContract", imgResponseUrl)
            self.setData({
              purchaseContract: wx.getStorageSync("purchaseContract")
            })
            if (self.data.purchaseContract != '') {
              self.setData({
                bg_purchaseContract: ''
              })
            }
          };
        } else if (e.target.id == '1') {
          self.setData({
            name: "invoice",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.bigShelfNum + '/' + "shouxu"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync("invoice", imgResponseUrl)
            self.setData({
              invoice: wx.getStorageSync("invoice")
            })
            if (self.data.invoice != '') {
              self.setData({
                bg_invoice: ''
              })
            }
          };
        } else if (e.target.id == '2') {
          self.setData({
            name: "certificate",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.bigShelfNum + '/' + "shouxu"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync("certificate", imgResponseUrl)
            self.setData({
              certificate: wx.getStorageSync("certificate")
            })
            if (self.data.certificate != '') {
              self.setData({
                bg_certificate: ''
              })
            }
          };
        } else if (e.target.id == '3') {
          self.setData({
            name: "purchaseContractSec",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.bigShelfNum + '/' + "shouxu"
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync("purchaseContractSec", imgResponseUrl)
            self.setData({
              purchaseContractSec: wx.getStorageSync("purchaseContractSec")
            })
            if (self.data.purchaseContractSec != '') {
              self.setData({
                bg_purchaseContractSec: ''
              })
            }
          };
        }
      }
    });
  },
  submit: function (e) {
    var that = this;
    if (that.data.isSubmit) {
      that.setData({
        isSubmit: false
      })
      var identityNum = that.data.identityNum;
      var contractForBuyImg = that.data.purchaseContract;
      if (contractForBuyImg == "" || contractForBuyImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var contractSecImg = that.data.purchaseContractSec;
      var invoiceForBuyImg = that.data.invoice;
      if (invoiceForBuyImg == "" || invoiceForBuyImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var archivesImg = that.data.certificate;
      if (archivesImg == "" || archivesImg == null) {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var bigShelfNum = that.data.bigShelfNum;
      wx.request({
        url: app.globalData.requestURL +'billOfDocumentImg/save',
        method: 'POST',
        data: {
          bigShelfNum: bigShelfNum,
          identityNum: identityNum,
          contractForBuyImg: contractForBuyImg,
          invoiceForBuyImg: invoiceForBuyImg,
          archivesImg: archivesImg,
          contractSecImg: contractSecImg
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.status == '0') {
            wx.setStorageSync('purchaseContractSec', '');
            wx.setStorageSync('purchaseContract', '');
            wx.setStorageSync('invoice', '');
            wx.setStorageSync('certificate', '');
            wx.request({
              url: app.globalData.requestURL +'registerLicense/selectTransforRegister',//等查询上户表的接口
              method: 'GET',
              data: {
                factoryNum: bigShelfNum,
                identification: identityNum
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (resf) {
                if (resf.data.status == '0') {//没有弹是否支付
                  wx.showToast({
                    title: '添加成功！',
                    icon: 'success',
                    duration: 2000,
                    mask: true,
                    success: function () {
                      wx.showModal({
                        title: '业务办理',
                        content: '是否办理设备登记',
                        success: function (res2) {
                          if (res2.confirm) {
                            that.wxPay();
                          } else if (res2.cancel) {
                            wx.redirectTo({
                              url: '/page-wode/deviceInformation/deviceList/deviceList'
                            })
                          }
                        }
                      })
                    }
                  });
                } else if (resf.data.status == '1') {
                  wx.redirectTo({
                    url: '/page-wode/deviceInformation/deviceList/deviceList'
                  })
                }
              }
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '不可多次提交',
        icon: 'loading',
        duration: 2000,
        mask: true
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      bigShelfNum: wx.getStorageSync('factoryNum'),
      identityNum: wx.getStorageSync('identification')
    });
    var bigShelfNum = that.data.bigShelfNum;
    var identityNum = that.data.identityNum;
    if (wx.getStorageSync("certificate") == null || wx.getStorageSync("certificate") == '') {
      that.setData({
        bg_certificate: app.globalData.registerImage[4] //合格证书或档案证书
      })
    }
    if (wx.getStorageSync("invoice") == null || wx.getStorageSync("invoice") == '') {
      that.setData({
        bg_invoice: app.globalData.registerImage[5] //采购发票照片
      })
    }
    if (wx.getStorageSync("purchaseContract") == null || wx.getStorageSync("purchaseContract") == '') {
      that.setData({
        bg_purchaseContract: app.globalData.registerImage[6] //采购合同照片
      })
    }
    if (wx.getStorageSync("purchaseContractSec") == null || wx.getStorageSync("purchaseContractSec") == '') {
      that.setData({
        bg_purchaseContractSec: app.globalData.registerImage[6]
      })
      that.setData({
        bg_purchaseContractSec: app.globalData.registerImage[6]
      })
    }

    //查询此照片表是否有提交过照片
    wx.request({
      url: app.globalData.requestURL +'billOfDocumentImg/getBillOfDocumentByCondition',//等查询此设备的手续的接口,
      method: 'POST',
      data: {
        bigShelfNum: bigShelfNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },

      success: function (res) {
        if (res.data.status == '1') {
          wx.redirectTo({
            url: '/page-wode/deviceInformation/deviceList/deviceList'
          })
        }
      }
    })
  },

  wxPay: function () {
    var that = this;
    app.wxPay(that.data.payData);
    app.signReadyCallback = sign => {
      that.requestPayment(sign);
    }
  },
  //申请支付
  requestPayment: function (obj) {
    var that = this;
    var data = obj;
    var factoryNum = that.data.bigShelfNum;
    var identification = wx.getStorageSync("identification");
    wx.requestPayment({
      'timeStamp': data.timeStamp,
      'nonceStr': data.nonceStr,
      'package': data.package,
      'signType': data.signType,
      'paySign': data.paySign,
      'success': function (res) {
        wx.request({
          url: app.globalData.requestURL +'registerLicense/save',
          method: 'POST',
          data: {
            factoryNum: factoryNum,
            identification: identification,
            state: '0'
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (resp) {
            if (resp.data.status == '0') {
              wx.showToast({
                title: '登记成功！',
                icon: 'success',
                duration: 2000,
                mask: true,
                success: function () {
                  wx.redirectTo({
                    url: '/page-wode/deviceInformation/deviceList/deviceList'
                  })
                }
              });

            } else {
              wx.showToast({
                title: '登记失败!',
                icon: 'loading',
                duration: 2000,
                mask: true//是否显示透明蒙层
              });
            }
          }
        })
      },
      'fail': function (res) {
        wx.showToast({
          title: '付款失败哦！',
          icon: 'loading',
          duration: 2000,
          mask: true,
          success: function () {
            wx.redirectTo({
              url: '/page-wode/deviceInformation/deviceList/deviceList'
            })
          }
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})